package com.lcg.weixin.service;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import org.jsoup.Connection;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Component;

import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

/**
 * @author: LCG
 * @Date: 2021-11-07 13:16:50
 * @description:
 **/
@Component
public class TestWeiXinService {

    @Autowired
    private RedisTemplate<String,String> redisTemplate;

    
    
    public void getWeixinAccessToken() throws IOException {
        Map<String,String> mapp=new HashMap<>();
        mapp.put("grant_type","client_credential");
        mapp.put("appid","wx93fa4c164d5d6f6c");
        mapp.put("secret","d46cf0cf65b6b79a06df2ce9e2b9ea09");

        //获取accessToekn
        String body = Jsoup.connect("https://api.weixin.qq.com/cgi-bin/token").ignoreContentType(true)
                .data(mapp).execute().body();
        JSONObject jsonObject = JSON.parseObject(body);

        //发送模板消息
        //url  https://www.douyin.com/video/7027013346209680672
        // POST https://api.weixin.qq.com/cgi-bin/message/template/send?access_token=ACCESS_TOKEN
        Map<String,Object> mmin=new HashMap<>();
        mmin.put("touser","o8f0I6wbZGg8YfK0rTKGKzVqPMek");
        mmin.put("template_id","yrRByR2y39dQ6l_30rpM-quUdalvh8Sjgy1lf5ZciEg");
        mmin.put("url","https://www.douyin.com/video/7027013346209680672");
        Map<String,Object> data=new HashMap<>();
        Map<String,Object> first=new HashMap<>();
        first.put("value","视频爆单通知");
        first.put("color","#173177");
        data.put("first",first);
        Map<String,Object> vedioId=new HashMap<>();
        vedioId.put("value","7027013346209680672");
        vedioId.put("color","#173177");
        data.put("keyword1",vedioId);
        Map<String,Object> detail=new HashMap<>();
        detail.put("value","这是饰品店额详情信息");
        detail.put("color","#173177");
        data.put("keyword2",detail);
        data.put("keyword3",detail);
        data.put("keyword4",detail);

        Map<String,Object> remark=new HashMap<>();
        remark.put("value","这是备注信息");
        remark.put("color","#173177");
        data.put("remark",remark);
        mmin.put("data",data);

        Map<String,String> dataooo=new HashMap<>();
        dataooo.put("access_token",jsonObject.getString("access_token"));
        String body1 = Jsoup.connect("https://api.weixin.qq.com/cgi-bin/message/template/send")
                .ignoreContentType(true)
                .data(dataooo)
                .requestBody(JSON.toJSONString(mmin))
                .method(Connection.Method.POST)
                .execute().body();


        System.out.println("==body1==>"+body1);

    }

}
